<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form :model="dataForm" ref="dataForm" label-width="0px" style="width:100%">
      <el-form-item prop="groupName" style="width:100%">
        <el-input v-model="dataForm.groupName" placeholder="请输入分组名称" style="width:100%"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
  data() {
    return {
      visible: false,
      active: 0,
      pfs: [],
      stores: [],
      dataForm: {
        id: 0,
        groupName: '',
        pfId: null,
        storId: null
      },
      dataRule: {
        groupName: [
          { required: true, message: '分组名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 选择销售平台获取销售平台下的店铺
    pfChange(value) {
      this.dataForm.storId = null
      this.$http({
        url: this.$http.adornUrl('/op/group/store'),
        method: 'get',
        params: this.$http.adornParams({
          'dtId': this.dataForm.dtId,
          "pf": this.dataForm.pfId
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.stores = data.list
        } else {
          this.stores = []
        }
      })
    },
    init(id, dtId, pfs) {
      this.dataForm.id = id
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.dataForm.dtId = dtId
        this.pfs = pfs
        this.active = 0
        //修改
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/op/group/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.groupName = data.group.groupName
              this.dataForm.dtId = data.group.dtId
              this.dataForm.pfId = data.group.blonPlantform
              this.pfChange(this.dataForm.pfId)
              this.dataForm.storId = data.group.storeId
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$http({
        url: this.$http.adornUrl("/op/group/savesingle"),
        method: 'post',
        data: this.$http.adornData({
          'id': this.dataForm.id,
          'groupName': this.dataForm.groupName
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.$message({
            message: data.msg,
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        } else {
          this.$message.error(data.msg)
        }
      })
    }
  }
}
</script>
